<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_item_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function countArr($arr,$query){
      $counter =0;
      foreach ($arr as $key ) {
          $counter++;
      }
      $arr[$counter] = $query;
      return $arr; 
    }

    public function insert_item($data){
        $this->db->query("call inv_items_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function update_item($data){
        $this->db->query("call inv_items_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function search_inv_itemDB($inum,$idsc,$ibo,$ityp,$igrp,$istat){
      $sql;$arg1="";$arg2="";$arg3="";$arg4="";$arg5="";$arg6="";
      $arr = array();

        $setter = 0;

        if($inum != ""){
          $arg1 = "item_num LIKE '".$inum."'"; $setter = 1;
          $arr = $this->countArr($arr,$arg1);
        }
        if($idsc != ""){
         $arg2 = "description LIKE '".$idsc."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg2);
        }
        if($ibo != ""){
         $arg3 = "base_uom LIKE '".$ibo."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg3);
        }
        if($ityp != ""){
         $arg4 = "item_type LIKE '".$ityp."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg4);
        }
        if($igrp != ""){
         $arg5 = "item_group LIKE '".$igrp."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg5);
        }
        if($istat != ""){
         $arg6 = "item_status LIKE '".$istat."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg6);
        }
        if ($setter == 1){
        $que = implode(" AND ",$arr);
        $sql = "SELECT * FROM inv_items WHERE ".$que ." ";
        }else{
          $sql = "SELECT * FROM inv_items WHERE item_num LIKE '".$inum."'";
        }
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }

    public function get_uom(){
      $query = $this->db->select('*')
                        ->from('inv_uom')
                        ->get();
      return $query->result(); 
    }
    public function get_item_data($id){
      $query = $this->db->select('*')
                        ->from('inv_items')
                        ->where('item_id',$id)
                        ->get();
      return $query->result();
    }
    public function get_inv_types(){
        $query = $this->db->query("SELECT * FROM inv_item_types_v");
      return $query->result(); 
    }
    public function get_inv_groups(){
        $query = $this->db->query("SELECT * FROM inv_item_groups_v");
      return $query->result(); 
    }
    public function get_cogs(){
        $query = $this->db->query("SELECT * FROM gl_cogs_acct_v");
      return $query->result(); 
    }
    public function get_expense(){
        $query = $this->db->query("SELECT * FROM gl_expense_acct_v");
     return $query->result(); 
    }
    public function get_sales(){
        $query = $this->db->query("SELECT * FROM gl_sales_acct_v");
     return $query->result(); 
    }
}